package com.aliyun.heiyu.powermaster.server.market;

import com.aliyun.heiyu.common.util.PropertiesUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.server.util.ShellUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class K8sManager {
    private static final Logger logger = LoggerFactory.getLogger(K8sManager.class);

    private static int sshPort;

    static {
        try {
            sshPort = StringUtil.convertInt(PropertiesUtil.getValue("serverIp.properties","sshPort"));
            } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String addNode(String masterIp, String ip){
        String cmd1 = getAddCmd(masterIp);
        if(cmd1 == null){
            return "操作失败，请重试";
        }
        String cmd2 = "ssh "+"-P"+sshPort+" root@"+ip+" "+cmd1;
        Map<String, String> map = ShellUtil.exec4(cmd2);
        if("0".equals(map.get("status"))){
            return "success";
        }
        return map.get("err");
    }

    public String deleteNode(String masterIp, String hostname, String ip){
        Map<String, String> map = drain(masterIp, hostname);
        if("0".equals(map.get("status"))){
            String cmd1 = "kubectl delete nodes "+hostname;
            String cmd2 = "ssh "+"-P"+sshPort+" root@"+masterIp+" "+cmd1;
            map = ShellUtil.exec4(cmd2);
        }
        if("0".equals(map.get("status"))){
            String cmd1 = "kubeadm reset -f";
            String cmd2 = "ssh "+"-P"+sshPort+" root@"+ip+" "+cmd1;
            map = ShellUtil.exec4(cmd2);
        }
        if("0".equals(map.get("status"))){
            return "success";
        }
        return map.get("err");
    }

    public String cordonNode(String masterIp, String hostName){
        String cmd1 = "kubectl cordon " + hostName;
        String cmd2 = "ssh "+"-P"+sshPort+" root@"+masterIp+" "+cmd1;
        Map<String, String> map = ShellUtil.exec4(cmd2);
        if("0".equals(map.get("status"))){
            return "success";
        }
        return map.get("err");

    }

    public String uncordonNode(String masterIp, String hostName){

        String cmd1 = "kubectl uncordon " + hostName;
        String cmd2 = "ssh "+"-P"+sshPort+" root@"+masterIp+" "+cmd1;
        Map<String, String> map = ShellUtil.exec4(cmd2);
        if("0".equals(map.get("status"))){
            return "success";
        }
        return map.get("err");
    }

    private String getAddCmd(String masterIp){
        StringBuffer stringBuffer = new StringBuffer();
        String s1 = "kubeadm join ";
        String hostAddress = masterIp;

        if(null == hostAddress){
            logger.error("get hostAddress exception");
            return null;
        }
        String s2 = hostAddress;
        String s3 = ":6443 --token ";
        String token = getToken(masterIp);
        if(null == token){
            logger.error("get token exception");
            return null;
        }
        String s4 = token.replaceAll("\r|\n", "");
        String s5 = " --discovery-token-ca-cert-hash sha256:";
        String caCert = getCACert(masterIp);
        if(null == caCert){
            logger.error("get CA-Cert exception");
            return null;
        }
        String s6 = caCert.replaceAll("\r|\n", "");
        String cmd1 = stringBuffer.append(s1).append(s2).append(s3).append(s4).append(s5).append(s6).toString();
        return cmd1;
    }

    private Map<String, String> drain(String masterIp, String hostName){
        String cmd1 = "kubectl drain " + hostName + " --delete-local-data --force --ignore-daemonsets";
        String cmd2 = "ssh "+"-P"+sshPort+" root@"+masterIp+" "+cmd1;
        Map<String, String> map = ShellUtil.exec4(cmd2);
        return map;
    }

    private String getCACert(String masterIp){
        String cmd1 = "openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'";
        String cmd2 = "ssh "+"-P"+sshPort+" root@"+masterIp+" "+cmd1;
        return ShellUtil.exec2(cmd2);
    }

    private String getToken(String masterIp){
        String cmd1 = "kubeadm token create";
        String cmd2 = "ssh "+"-P"+sshPort+" root@"+masterIp+" "+cmd1;
        return ShellUtil.exec2(cmd2);
    }


}
